﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GEA
{
    public partial class frmEntreposto : Form
    {

        private ArrayList Entrepostos;
        clDatabase dal = new clDatabase();
        Entreposto Ent = new Entreposto();

        public frmEntreposto()
        {
            InitializeComponent();
            
        }

        private void frmEntreposto_Load(object sender, EventArgs e)
        {
            


            frmEntreposto_ReLoad();

            //cores das grid's
            dgvEntrepostos.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;
            dgvEntrepostos.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dgvEntrepostos.ColumnHeadersDefaultCellStyle.Font = new Font(DataGridView.DefaultFont, FontStyle.Bold);
            //dgvEntrepostos.Columns[0].HeaderCell.Style.Font = new Font("Arial", 14F, FontStyle.Bold, GraphicsUnit.Pixel);


            int h = ((mdiMain)this.ParentForm).Height - 200 ;

            pnlEntrepostos.Height = h / 2;

            panel1.Height = h / 2;
            panel1.Location = new Point(panel1.Location.X, h / 2);

            dgvEntrepostos.Height = h / 2 - 40;
            this.Padding = new System.Windows.Forms.Padding(5, 20, 20, 50);
        }


        public void frmEntreposto_ReLoad()
        {
            
            ArrayList Lista_ent = new ArrayList();
            dgvEntrepostos.DataSource = null;
            pnlEntrepostos.Controls.Clear();


            Lista_ent = dal.Entrepostos_Get_Lista();
            
            //load datagrid
            dgvEntrepostos.DataSource = dal.Entrepostos_Get_Lista_dt();
            if (int.Parse(GlobalVar.GlobalValue.ToString()) != 0)
            {
                dgvEntrepostos.Columns["id_user"].Visible = false;
                dgvEntrepostos.Columns["data_criacao"].Visible = false;
                dgvEntrepostos.Columns["data_alteracao"].Visible = false;
                dgvEntrepostos.Columns["activo"].Visible = false;
                dgvEntrepostos.Columns["guid"].Visible = false;
            }
            else
            {
                dgvEntrepostos.Columns["id_user"].HeaderText = "Utilizador".ToUpper();
                dgvEntrepostos.Columns["data_criacao"].HeaderText = "Data Criação".ToUpper();
                dgvEntrepostos.Columns["data_alteracao"].HeaderText = "Data Alteração".ToUpper();
                dgvEntrepostos.Columns["activo"].HeaderText = "Activo".ToUpper();
            }
            dgvEntrepostos.Columns["id_entreposto"].HeaderText = "Nº Entreposto".ToUpper();
            dgvEntrepostos.Columns["tipo"].HeaderText = "Tipo".ToUpper();
            dgvEntrepostos.Columns["nome_firma"].HeaderText = "Nome da Firma".ToUpper();



            if (Lista_ent.Count > 0)
            {
                ((mdiMain)this.ParentForm).tsbNovo.Enabled = true;
                ((mdiMain)this.ParentForm).tsbAlterar.Enabled = true;
                ((mdiMain)this.ParentForm).tsbApagar.Enabled = true;
                


                //para passar dados para outra form
                Entrepostos = Lista_ent;

                //Get N Entrepostos from FB
                int n = Lista_ent.Count;
                int width_botao = 120;
                int height_botao = 120;
                int top_botao = 60;
                int width_screen = pnlEntrepostos.Width / (n + 1);



                for (int i = 0; i < n; i++)
                {
                    Entreposto ent = (Entreposto)Lista_ent[i];
                    var Botao = new Button();
                    Botao.Width = width_botao;
                    Botao.Height = height_botao;
                    Botao.Name = "btn_" + i;
                    Botao.Location = new Point((i + 1) * width_screen - (Botao.Width / 2), top_botao);
                    Botao.Text = ent.Nome_firma + Environment.NewLine + Environment.NewLine + ent.Id_entreposto;
                    Botao.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
                    Botao.Click += new EventHandler(btnSubmit_Click);
                    pnlEntrepostos.Controls.Add(Botao);
                }
            }
            else
            {
                pnlEntrepostos.Controls.Clear();
                ((mdiMain)this.ParentForm).tsbAlterar.Enabled = false;
                ((mdiMain)this.ParentForm).tsbApagar.Enabled = false;
            }
            

        }


        private void btnSubmit_Click(object sender, EventArgs e)
        {

            var x = ((System.Windows.Forms.Button)sender).Name;
            var z = x.Split('_');
            int n = int.Parse(z[1]);
            Entreposto ent = (Entreposto)Entrepostos[n];

            ((mdiMain)this.ParentForm).startup_processo(ent);           
        }




        private void dgvEntrepostos_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            DataGridViewCell cell = null;
            foreach (DataGridViewCell selectedCell in dgvEntrepostos.SelectedCells)
            {
                cell = selectedCell;
                break;
            }
            if (cell != null)
            {
                DataGridViewRow row = cell.OwningRow;

                Ent.Id_user = int.Parse(GlobalVar.GlobalValue.ToString());
                Ent.Data_criacao = DateTime.Parse(row.Cells["data_criacao"].Value.ToString());
                Ent.Data_alteracao = DateTime.Parse(row.Cells["data_alteracao"].Value.ToString());
                Ent.Id_entreposto = row.Cells["id_entreposto"].Value.ToString();
                Ent.Nome_firma = row.Cells["nome_firma"].Value.ToString();
                Ent.Tipo = row.Cells["tipo"].Value.ToString();
                ((mdiMain)this.ParentForm).startup_processo(Ent);

            }
        }

        private void dgvEntrepostos_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            e.Column.HeaderText = e.Column.HeaderText.ToUpper();
        }





    }
}
